﻿using System;
using System.Collections;
using System.Linq;
using System.Reflection;
using UnityEngine;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Net;
using UnityEngine.Networking;
#if WEBGL
using EntryEngine.Unity;
#endif

public class Main : MonoBehaviour
{
    internal static string PersistentDataPath;
    internal static string DataPath;
    internal const string VERSION = "__version.txt";
    internal const string FILE_LIST = "__filelist.txt";
    internal const string RUNTIME = "UnityRuntime.bytes";
    static string[] SPLIT = new string[] { "\r\n" };

    /// <summary>引擎渲染用的默认材质，请在Unity编辑器中选择Sprites-Default材质</summary>
    public Material GLMaterial;
    /// <summary>热更新服务器地址，例如http://127.0.0.1:9901/，地址目录应该是StreamingAssets</summary>
    public string ServerUrl = "http://127.0.0.1:9901/";
    /// <summary>热更新是否更新全部资源，不更新全部则仅更新程序，其它资源交由程序内自定义热更新页面进行更新</summary>
    public bool All = true;

    Texture2D logo;
    GUIContent text = new GUIContent("正在启动引擎");
    Texture2D process;
    /// <summary>总共需要下载的字节数</summary>
    long total;
    /// <summary>当前下载的字节数</summary>
    long download;

    /// <summary>一个需要新下载或更新的文件</summary>
    public class FileNew
    {
        /// <summary>文件路径</summary>
        public string File;
        /// <summary>文件最后修改时间</summary>
        public string Time;
        /// <summary>文件大小，单位字节</summary>
        public long Length;

        /// <summary>文件下载时的异常</summary>
        public Exception UpdateException { get; set; }

        /// <summary>可以生成文件列表中一个文件的格式</summary>
        public override string ToString()
        {
            return string.Format("{0}\t{1}\t{2}\r\n", File, Time, Length);
        }
    }
    class FileDownload
    {
        public FileNew File;
        public UnityWebRequest Web;
    }
    /// <summary>本地资源加载</summary>
    class Loader
    {
        private byte[] _bytes;
        /// <summary>加载好的资源</summary>
        internal byte[] Bytes
        {
            get
            {
                if (Request == null)
                    return _bytes;
                else
                {
                    if (string.IsNullOrEmpty(Request.error))
                        return Request.downloadHandler.data;
                    else
                        return null;
                }
            }
        }
        internal UnityWebRequest Request;
        /// <summary>需要异步等待的对象</summary>
        internal UnityWebRequestAsyncOperation Wait { get; private set; }
        /// <summary>加载本地资源</summary>
        /// <returns>true: 不需要异步等待 / false: 需要异步等待</returns>
        public bool Load(string file)
        {
            _bytes = null;
            Request = null;
            if (File.Exists(PersistentDataPath + file))
            {
                _bytes = File.ReadAllBytes(PersistentDataPath + file);
                return true;
            }
            else
            {
                string target = DataPath + file;
                if (File.Exists(target))
                {
                    _bytes = File.ReadAllBytes(target);
                    return true;
                }
                Request = UnityWebRequest.Get(target);
                Wait = Request.SendWebRequest();
                return Request.isDone;
            }
        }
    }

    static string Byte2MB(long bytes)
    {
        double temp = bytes / 1048576.0;
        return temp.ToString("0.00");
    }
    static string ReadString(byte[] bytes)
    {
        using (MemoryStream stream = new MemoryStream(bytes))
        {
            using (StreamReader reader = new StreamReader(stream))
            {
                return reader.ReadToEnd();
            }
        }
    }
    static void WriteFile(string file, byte[] data)
    {
        file = PersistentDataPath + file;
        string dir = Path.GetDirectoryName(file);
        if (!Directory.Exists(dir))
            Directory.CreateDirectory(dir);
        File.WriteAllBytes(file, data);
    }
    // Use this for initialization
    void Start()
    {
        DataPath = Application.streamingAssetsPath + '/';
        if (Application.platform != RuntimePlatform.WindowsEditor)
        {
            PersistentDataPath = Application.persistentDataPath + '/';
            Environment.CurrentDirectory = PersistentDataPath;
        }
        else
            PersistentDataPath = DataPath;

        // 初始化游戏开始过程
#if WEBGL
            if (Application.platform == RuntimePlatform.WebGLPlayer)
            {
                var gate = gameObject.AddComponent<UnityGate>();
                gate.GLMaterial = this.GLMaterial;
                gate.Entry.ShowMainScene<MAIN>();
            }
            else
#endif
        {
            StartCoroutine(StartCoroutine());
        }
    }
    System.Collections.IEnumerator StartCoroutine()
    {
        Loader loader = new Loader();

        #region 加载LOGO

        Debug.Log("正在加载引擎LOGO");

        GUIStyle.none.fontSize = 24;
        GUIStyle.none.normal.textColor = Color.white;

        process = new Texture2D(1, 1);
        process.SetPixel(0, 0, Color.white);

        if (!loader.Load("LOGO.png"))
            yield return loader.Wait;
        if (loader.Bytes != null)
        {
            logo = new Texture2D(1, 1);
            if (!ImageConversion.LoadImage(logo, loader.Bytes, false))
            {
                Resources.UnloadAsset(logo);
                logo = null;
            }
        }
        if (logo == null)
        {
            logo = new Texture2D(1, 1);
            if (!ImageConversion.LoadImage(logo,
                new byte[]{137,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,0,241,0,0,1,5,8,6,0,0,0,50,
191,1,178,0,0,25,72,73,68,65,84,120,156,237,157,123,144,101,69,125,199,191,203,75,94,214,56,128,2,130,
36,117,145,24,18,137,166,102,13,6,197,24,157,13,130,68,141,102,150,103,4,147,212,16,83,68,99,72,178,152,
252,145,68,147,212,78,76,82,49,106,116,199,36,130,132,93,216,241,133,130,34,187,34,15,133,84,193,4,131,70,
66,8,91,148,34,41,208,218,25,34,5,65,94,169,102,127,13,189,167,187,207,233,123,78,159,115,186,207,253,126,
170,110,45,220,115,230,220,238,62,253,237,223,175,127,253,90,179,186,186,10,66,12,94,7,224,90,22,72,30,76,
77,77,97,143,73,47,4,242,52,63,14,224,189,0,110,7,240,21,0,223,0,240,30,0,47,96,241,164,15,45,
241,228,242,124,0,235,1,156,1,224,4,85,23,28,37,241,24,128,47,2,248,132,252,251,152,117,7,233,21,101,
137,41,226,201,226,185,0,222,2,224,76,0,179,0,246,26,35,247,15,0,184,12,192,39,1,44,91,87,73,47,
80,196,147,193,115,0,156,12,224,116,0,111,2,176,95,132,92,127,83,172,243,165,34,110,210,19,20,241,112,81,
177,142,215,138,197,125,43,128,233,150,114,170,221,109,101,157,175,2,240,168,117,7,105,21,138,120,120,188,66,250,
184,167,1,120,97,199,185,219,9,96,51,128,139,232,110,119,7,69,60,12,94,34,194,85,86,247,152,68,114,164,
221,109,37,234,251,173,171,36,26,20,113,190,28,33,125,92,37,222,153,132,115,241,4,128,47,139,117,254,60,221,
237,248,80,196,121,113,16,128,183,137,197,125,141,244,123,235,240,16,128,43,0,220,9,224,125,29,150,0,221,237,
22,160,136,211,103,127,137,40,43,139,251,6,0,251,212,76,241,143,196,34,110,22,139,248,48,128,87,3,184,209,
186,179,27,190,45,193,176,75,0,220,55,216,183,215,1,20,113,154,236,13,96,157,8,87,141,233,30,88,51,149,
79,2,184,1,192,22,0,159,18,75,104,210,167,136,53,116,183,27,162,68,60,206,96,63,105,15,53,91,234,85,
226,42,207,1,56,164,193,47,221,38,227,183,151,3,184,215,186,154,22,123,2,56,69,62,59,37,205,202,58,223,
204,186,22,14,45,113,191,188,76,132,171,130,84,71,53,72,201,93,98,113,55,75,95,55,132,20,44,177,143,255,
20,235,76,119,187,2,186,211,253,48,18,87,89,125,126,186,65,10,84,229,222,42,194,189,197,186,90,77,202,34,
214,104,119,251,18,9,198,61,98,221,49,225,208,157,238,142,67,101,177,129,178,186,199,123,22,27,132,160,90,220,
79,139,112,175,151,74,62,100,76,119,251,65,113,183,47,162,187,189,59,180,196,237,49,101,44,54,120,189,84,200,
58,40,235,115,165,8,247,75,17,131,63,57,88,98,31,218,221,254,23,0,223,243,220,51,17,208,157,142,143,90,
108,240,70,17,174,250,119,223,154,191,160,44,236,54,233,231,126,22,192,15,173,59,154,147,179,136,53,42,2,127,
173,8,250,51,147,232,110,211,157,142,195,158,178,27,198,25,178,216,96,170,230,83,159,18,55,81,89,220,37,174,
14,10,66,77,120,153,149,207,196,186,219,180,196,245,57,222,88,108,112,88,131,231,124,75,132,171,172,238,61,214,
213,246,24,130,37,246,241,95,226,106,95,12,224,59,190,155,134,0,221,233,241,57,86,92,101,37,222,163,27,60,
231,30,17,237,22,89,44,208,7,67,22,177,102,240,238,54,221,233,48,94,36,227,184,74,188,47,111,240,156,239,
203,144,144,18,238,77,226,62,147,118,41,186,219,75,50,92,117,227,144,202,159,150,216,205,193,50,115,234,76,153,
73,85,119,177,129,10,72,125,78,220,229,237,0,30,183,238,232,143,73,176,196,62,238,22,87,59,123,119,155,238,
244,238,28,104,44,54,56,73,230,48,215,225,81,25,10,82,22,247,11,9,187,112,147,44,98,141,118,183,47,150,
198,246,33,235,142,196,161,59,189,75,168,39,137,197,85,2,62,192,186,35,12,85,25,174,19,139,171,250,94,43,
93,103,132,212,194,116,183,31,146,133,34,159,200,205,221,158,68,17,171,23,119,162,88,220,57,89,167,91,151,91,
196,226,94,206,57,190,217,163,60,177,115,229,163,221,237,247,231,144,169,73,116,167,175,151,69,245,117,185,211,88,
108,112,87,191,89,105,4,221,233,106,234,78,143,237,140,73,117,167,235,236,252,248,61,217,115,121,11,119,165,32,
169,49,233,125,226,50,118,26,139,13,110,144,126,47,33,201,65,17,239,206,195,178,195,132,178,184,87,203,182,54,
132,36,13,69,252,44,23,0,88,204,113,152,129,76,54,60,21,241,89,182,81,192,36,71,40,98,66,50,135,34,
38,36,115,40,98,66,50,135,34,38,36,115,40,98,66,50,135,34,38,36,115,40,98,66,50,135,34,38,36,115,
40,98,66,50,135,34,38,36,115,40,98,66,50,135,34,38,36,115,40,98,66,50,135,34,38,36,115,40,98,66,
50,135,34,38,36,115,40,98,66,50,135,34,38,36,115,40,98,66,50,135,34,38,36,115,40,98,66,50,135,34,
38,36,115,40,98,66,50,135,34,38,36,115,40,98,66,50,135,34,38,36,115,40,98,66,50,135,34,38,36,115,
40,98,66,50,135,34,38,36,115,40,98,66,50,135,34,38,36,115,40,98,66,50,135,34,38,36,115,40,98,66,
50,135,34,38,36,115,40,98,66,50,135,34,38,36,115,40,98,66,50,135,34,38,36,115,134,38,226,163,1,156,
111,125,75,200,128,201,93,196,251,0,120,29,128,191,5,112,7,128,255,6,240,33,235,174,230,60,175,235,140,17,
18,202,94,25,150,212,145,0,222,0,224,20,0,235,0,28,104,221,17,159,27,229,247,190,219,117,102,9,169,34,
7,17,239,9,224,231,69,68,74,188,63,107,221,209,62,47,5,112,147,164,225,155,61,252,62,25,159,111,0,184,
28,192,127,0,216,95,234,208,89,0,14,25,90,89,166,42,226,231,3,56,9,192,169,98,109,15,178,238,232,158,
35,197,34,255,10,128,175,38,144,30,226,230,71,0,222,13,96,19,128,167,140,59,148,160,255,84,190,95,239,252,
203,76,73,69,196,170,111,62,35,150,86,9,119,109,162,253,245,41,0,87,3,56,23,192,22,235,42,73,129,121,
0,23,123,210,177,10,224,12,177,204,167,90,87,51,165,79,17,43,65,252,146,225,38,31,102,221,145,38,42,152,
118,169,88,230,15,100,146,230,73,225,250,18,1,107,158,4,240,78,241,240,158,99,93,205,144,174,69,124,156,97,
109,79,200,52,176,166,88,3,224,175,68,200,191,7,224,9,235,14,210,7,151,4,254,230,189,0,182,3,120,163,
117,37,67,218,22,209,254,210,226,233,104,242,81,214,29,121,243,46,0,71,0,56,27,192,255,13,44,111,57,50,
78,208,241,14,138,216,207,139,165,112,148,104,95,3,96,95,239,157,195,224,109,210,21,120,19,128,157,3,207,107,
234,140,19,71,25,204,68,167,24,34,222,87,196,122,178,184,201,47,182,238,24,62,175,2,240,53,105,184,238,153,
192,252,167,130,26,126,252,215,192,180,252,140,245,77,166,212,21,241,145,34,88,229,38,207,2,56,192,186,99,242,
56,86,198,146,149,23,114,219,164,23,70,79,188,29,192,199,10,67,75,46,148,161,121,173,227,251,44,25,215,165,
80,125,220,235,100,230,210,71,1,188,153,2,222,141,195,37,66,186,206,186,66,186,224,149,1,115,231,213,232,194,
199,51,14,170,90,140,43,226,135,37,152,115,191,117,133,104,158,11,224,42,177,10,164,123,254,14,192,159,136,88,
139,28,33,239,102,48,86,24,53,91,163,219,1,252,130,132,232,143,180,174,18,197,222,0,46,2,240,34,0,127,
193,18,233,148,61,100,102,214,175,3,248,52,128,187,229,125,28,47,158,227,126,67,203,112,93,151,226,78,0,39,
2,248,10,128,145,117,149,64,198,146,255,92,26,186,243,57,150,220,57,106,56,243,61,147,144,209,38,97,246,123,
68,200,119,88,87,136,201,111,1,248,140,196,19,8,137,78,211,177,178,251,164,127,193,104,108,57,106,12,249,218,
33,174,160,33,253,19,99,192,251,1,89,152,31,58,62,55,169,28,47,67,80,71,79,122,65,144,184,196,154,181,
178,42,195,42,215,89,87,136,201,49,0,190,14,224,21,44,21,18,139,152,83,207,30,146,25,75,95,178,174,16,
147,67,101,61,242,41,44,21,18,131,216,243,71,31,1,240,22,9,237,19,63,106,130,204,21,0,126,195,123,7,
33,129,180,49,9,92,237,172,112,218,24,203,194,38,21,53,188,247,143,50,166,73,72,109,218,90,201,241,132,236,
126,241,49,235,10,41,162,102,23,253,211,144,166,1,146,110,105,115,57,150,218,65,225,183,1,252,181,117,133,20,
81,179,139,62,223,209,206,157,100,96,180,189,166,82,173,38,249,3,0,127,102,93,33,69,78,150,128,215,161,214,
21,66,74,232,106,97,180,234,247,253,97,192,18,177,73,103,173,140,37,255,196,164,23,4,9,167,203,221,13,62,
32,238,53,133,92,206,72,198,146,95,89,122,23,33,66,215,91,148,168,64,215,57,92,12,80,201,33,50,77,243,
205,137,167,147,36,64,31,251,12,169,161,167,211,101,40,138,248,217,79,198,219,223,233,189,131,76,60,232,113,179,
176,79,201,73,10,143,88,87,136,137,58,194,230,31,0,252,165,44,109,36,196,162,207,29,255,190,40,251,81,61,
100,93,33,69,222,43,155,12,184,118,171,32,19,78,223,219,118,126,85,78,129,88,181,174,144,34,106,187,159,43,
101,251,31,66,158,33,133,189,119,111,150,165,140,223,183,174,144,34,235,228,80,183,195,173,43,100,98,73,101,3,
237,219,100,223,174,251,172,43,164,200,203,164,225,251,41,235,10,153,72,82,218,5,255,14,217,132,158,155,175,87,
243,99,98,145,79,76,61,161,164,125,82,59,202,226,110,17,242,157,214,21,82,68,157,217,124,141,28,35,67,38,
152,20,207,163,249,174,184,214,60,145,191,26,117,132,206,86,217,11,156,76,40,169,30,42,117,191,108,192,119,171,
117,133,20,81,239,240,131,178,90,140,99,201,19,72,202,39,195,169,19,6,95,47,125,63,82,205,5,0,54,15,
229,224,108,18,78,234,199,59,254,175,28,218,118,141,117,133,184,56,93,246,56,123,158,227,26,25,40,57,156,209,
250,176,236,219,124,133,117,133,184,248,69,241,94,120,196,206,132,144,203,65,203,143,2,248,85,0,91,172,43,196,
197,75,101,44,249,56,199,53,50,48,114,58,45,253,113,0,191,38,155,203,145,106,148,37,190,65,44,51,25,48,
57,137,24,178,14,121,30,192,223,91,87,136,11,213,55,190,90,250,202,100,160,228,38,98,200,206,32,239,150,229,
121,164,154,125,36,106,253,251,44,171,97,146,163,136,53,127,44,75,244,72,53,107,100,123,164,15,102,254,206,137,
131,220,95,232,70,153,173,196,125,187,194,120,151,204,240,218,55,135,196,146,48,134,208,42,127,8,192,111,114,223,
174,96,212,92,235,109,0,166,51,73,47,169,96,40,174,213,63,3,56,27,192,99,214,21,226,226,231,120,94,214,
112,24,82,255,232,50,25,75,126,212,186,66,32,158,138,110,228,84,176,107,111,150,202,48,24,90,144,67,29,133,
114,170,245,237,100,163,119,21,221,147,194,29,38,67,140,84,110,183,190,153,60,204,237,128,185,185,222,192,225,112,
195,112,120,220,200,73,29,225,254,0,192,71,1,220,34,135,225,145,76,224,113,154,121,243,164,140,1,175,169,249,
46,213,118,193,159,147,120,194,53,70,159,249,40,57,169,67,125,142,182,254,138,36,5,45,113,126,60,101,140,139,
239,81,99,35,0,229,106,127,1,192,25,114,2,163,154,143,126,85,33,178,255,29,0,239,7,112,140,236,178,242,
113,0,15,90,79,34,73,64,17,231,131,118,113,215,212,16,174,250,219,235,100,222,249,225,178,180,243,50,89,230,
89,198,83,178,136,66,255,221,217,18,115,160,187,157,16,116,167,211,230,73,163,161,173,211,224,254,155,204,155,190,
28,192,189,214,213,241,80,71,238,92,42,31,237,110,159,5,224,37,131,44,249,140,160,37,78,15,115,10,105,157,
247,115,23,128,247,1,248,73,0,51,0,254,38,130,128,139,104,119,91,253,198,9,0,22,233,110,247,7,45,113,
122,212,217,236,238,62,153,19,189,89,162,203,93,114,179,124,126,23,192,91,1,156,43,39,122,208,64,116,4,69,
156,47,171,50,117,114,179,244,119,251,238,167,154,238,246,11,37,96,118,174,88,107,210,34,108,45,243,226,17,177,
184,234,88,216,195,100,225,199,181,9,6,154,148,103,176,0,224,88,186,219,237,67,17,167,207,227,178,59,199,57,
50,36,116,154,140,237,230,50,71,92,185,218,231,73,116,251,116,57,210,150,43,206,34,66,119,58,77,84,112,235,
38,217,24,112,9,192,3,3,200,211,35,18,37,191,156,238,118,92,104,137,211,226,91,0,254,8,192,8,192,171,
1,124,100,32,2,46,82,116,183,63,34,135,5,144,26,208,18,247,207,61,98,113,55,139,136,39,13,29,221,190,
64,38,161,40,235,124,146,172,186,34,1,80,196,253,240,128,184,201,91,196,109,230,246,66,187,250,248,75,242,209,
238,246,217,178,135,54,41,129,238,116,53,7,71,122,206,15,1,124,18,192,201,0,142,0,112,62,128,175,83,192,
78,180,187,173,54,191,95,11,224,195,116,183,253,80,196,110,14,4,112,38,128,43,1,252,143,243,142,48,30,149,
72,242,105,18,89,62,71,34,205,143,55,120,230,164,177,12,224,119,196,58,175,103,116,219,134,238,244,179,168,93,
47,126,89,86,247,168,190,217,1,214,29,97,232,197,6,155,101,50,198,106,47,185,25,30,166,187,253,2,153,183,
253,14,30,85,3,172,89,93,157,184,58,118,187,231,197,171,33,144,253,172,111,195,185,69,132,187,85,220,65,210,
13,51,18,12,83,158,211,65,145,127,49,249,243,158,167,166,166,40,226,134,220,41,194,221,34,11,15,72,127,168,
115,153,79,145,46,203,41,145,246,19,203,66,196,116,167,199,231,94,153,176,176,89,150,250,145,52,80,238,246,103,
229,51,81,238,54,45,113,24,59,141,197,6,55,112,81,124,86,204,200,112,213,25,34,238,113,160,59,157,40,161,
34,126,88,182,192,85,194,253,114,97,7,73,146,31,123,139,155,253,142,49,220,109,186,211,25,242,152,108,24,167,
250,184,87,200,70,114,100,56,239,246,10,249,104,119,251,237,0,94,110,221,153,25,180,196,187,38,91,124,205,88,
108,240,3,235,47,200,144,57,78,172,243,89,14,119,155,238,116,162,104,17,255,187,184,202,151,201,118,51,100,178,
113,185,219,116,167,19,229,195,98,121,191,61,129,121,39,126,76,119,251,96,9,132,101,193,36,90,98,66,6,131,
178,196,156,59,77,72,230,80,196,132,100,14,69,76,72,230,80,196,132,100,14,69,76,72,230,80,196,132,100,14,
69,76,72,230,80,196,132,100,14,69,76,72,230,80,196,132,100,14,69,76,72,230,80,196,132,100,14,69,76,72,
230,80,196,132,100,14,69,76,72,230,80,196,132,100,14,69,76,72,230,80,196,132,100,14,69,76,72,230,80,196,
132,100,14,69,76,72,230,80,196,132,100,14,69,76,72,230,80,196,132,100,14,69,76,72,230,80,196,132,100,14,
69,76,72,230,80,196,132,100,14,69,76,72,230,80,196,132,100,14,69,76,72,230,80,196,132,100,14,69,76,72,
230,80,196,132,100,14,69,76,72,230,80,196,132,100,14,69,76,72,230,80,196,132,100,14,69,76,72,230,80,196,
132,100,14,69,76,72,230,80,196,132,100,14,69,76,72,230,80,196,132,100,14,69,76,72,230,80,196,132,100,206,
94,124,129,132,140,197,168,228,230,29,242,111,200,61,154,178,123,87,228,83,74,93,17,207,0,216,104,125,59,62,
42,67,231,69,120,14,105,143,105,249,180,129,89,161,103,1,108,11,252,141,197,30,235,205,6,0,243,214,183,187,
242,114,180,252,247,168,36,47,107,1,44,27,255,175,116,52,103,221,181,139,243,36,175,165,212,21,241,70,41,244,
24,164,40,226,178,214,177,79,130,90,230,200,108,244,84,218,166,44,75,133,214,108,7,176,174,164,242,155,44,91,
223,180,71,168,193,154,46,164,125,197,211,248,153,207,90,112,88,102,147,185,18,129,63,205,131,15,62,184,88,71,
196,179,17,5,12,73,228,146,245,109,191,148,181,164,125,114,161,188,248,46,105,171,65,115,85,222,80,113,186,254,
182,45,102,2,235,251,116,224,125,230,61,11,21,13,100,200,243,150,234,4,182,54,88,223,52,35,102,131,16,139,
237,34,152,212,232,178,242,106,218,18,177,203,163,152,177,190,113,179,221,249,109,59,132,166,169,14,179,30,107,61,
14,203,227,90,226,216,86,24,45,23,210,208,232,210,141,212,180,213,31,118,229,37,164,193,112,137,191,77,218,108,
196,124,174,114,49,143,190,119,176,50,53,53,53,182,136,167,107,88,168,249,138,130,72,85,196,101,105,238,139,62,
44,241,90,235,155,106,182,5,148,159,43,47,33,117,161,107,17,175,43,252,255,38,143,11,188,206,240,16,230,229,
190,34,102,64,174,44,144,183,206,104,228,54,148,244,201,15,66,141,192,86,157,190,235,200,147,105,147,84,251,197,
85,44,69,114,187,231,3,186,41,174,74,223,5,117,126,215,103,57,76,92,46,113,72,153,247,85,14,35,67,152,
197,180,95,88,240,44,22,67,162,202,158,186,179,82,120,150,175,76,158,41,135,46,198,137,183,7,136,120,54,83,
17,111,239,176,82,245,85,121,199,101,20,32,98,159,53,77,89,196,91,75,60,133,91,173,111,202,89,47,207,114,
233,98,197,168,87,179,37,46,247,51,229,80,119,198,150,50,239,79,85,124,180,171,224,106,113,139,248,10,167,79,
66,172,73,172,10,149,114,229,29,151,38,46,113,72,153,187,250,210,109,179,33,114,29,221,33,86,120,201,24,135,
215,159,145,52,10,243,162,33,87,153,172,152,163,20,117,44,241,180,167,5,41,162,93,133,21,73,116,89,69,77,
77,196,161,19,28,98,69,73,203,202,70,211,135,136,183,182,52,122,224,203,75,151,13,103,40,163,146,174,142,175,
49,210,248,242,163,27,34,221,63,46,234,105,218,211,167,214,191,185,182,169,59,189,177,36,113,154,165,66,139,185,
28,80,81,83,234,23,119,29,96,73,177,242,66,202,161,141,232,180,43,47,161,141,69,215,150,120,147,167,12,182,
87,52,226,203,162,149,98,93,42,230,221,39,100,23,197,9,50,79,83,39,58,61,142,21,214,44,151,248,246,154,
153,132,68,220,245,80,71,138,150,120,186,197,8,189,75,136,161,101,222,101,116,122,78,234,165,249,155,90,208,85,
195,173,23,122,242,228,122,143,33,66,94,146,190,180,197,184,125,98,159,137,47,254,88,49,161,33,226,44,43,144,
174,9,177,196,174,151,81,135,84,45,80,155,239,195,85,118,41,14,47,45,201,48,142,254,20,235,241,178,97,145,
139,121,218,225,177,224,174,188,79,87,228,127,197,97,24,159,97,28,75,60,10,176,166,240,204,133,222,81,50,151,
212,124,126,42,116,105,25,83,237,15,135,136,184,174,168,92,13,82,89,221,208,244,29,220,43,166,113,193,200,203,
166,194,187,244,229,199,181,138,233,214,146,251,245,179,110,45,246,133,53,227,136,216,55,224,108,178,88,242,98,151,
43,42,198,116,66,253,226,16,97,185,42,98,91,191,101,189,184,14,168,74,151,179,127,214,128,28,34,244,197,52,
110,181,238,168,166,184,114,107,107,133,128,53,94,33,135,186,211,51,1,86,184,212,228,87,4,1,204,223,73,129,
212,134,151,124,13,99,155,84,189,139,216,238,125,14,34,14,13,242,237,240,24,172,21,195,72,205,149,12,33,237,
240,44,116,209,66,222,173,172,66,45,113,83,43,140,192,151,158,147,136,99,85,226,84,135,85,170,210,21,123,17,
66,14,238,116,113,10,166,15,223,12,60,109,228,202,166,82,154,30,206,138,227,62,203,34,135,136,56,100,209,67,
149,21,134,188,244,170,126,113,10,34,14,237,11,150,53,88,227,144,162,5,10,45,131,58,113,12,87,217,165,26,
220,43,98,190,135,178,188,111,119,164,117,135,81,102,59,42,162,215,102,3,176,80,208,140,245,236,16,17,187,90,
148,34,33,243,68,117,2,203,132,170,215,100,118,185,212,172,136,171,96,139,12,125,120,169,236,29,105,124,147,247,
171,112,237,86,17,218,24,244,97,137,239,78,44,232,106,69,194,171,250,196,177,172,176,38,164,37,237,123,168,169,
203,225,165,20,215,207,34,209,225,37,215,223,117,65,153,231,216,7,86,57,84,137,216,231,183,155,184,58,224,62,
114,8,110,113,206,116,187,150,199,85,7,82,238,15,167,38,98,203,16,150,185,211,115,1,130,90,25,83,196,33,
195,71,125,139,120,210,135,151,186,182,194,72,184,49,171,90,63,191,221,211,181,218,20,80,142,230,50,214,70,121,
43,19,113,136,21,46,203,160,143,170,197,16,125,247,139,39,125,120,105,156,238,145,201,116,64,252,196,151,151,148,
231,142,151,229,201,119,45,36,63,179,53,150,48,66,111,4,96,226,19,241,92,224,206,12,161,1,45,147,208,245,
197,125,137,120,210,45,241,114,205,252,133,204,169,247,229,37,229,110,69,91,238,116,157,231,58,27,65,95,159,56,
118,95,216,36,229,241,98,14,47,213,167,238,220,231,148,135,151,82,91,34,235,42,63,167,37,174,218,19,11,13,
172,48,164,47,80,181,144,162,175,194,227,240,82,125,234,78,144,73,121,120,105,186,230,251,14,41,139,58,207,117,
149,159,83,196,62,63,223,164,201,190,82,33,155,4,244,213,47,230,240,82,125,234,150,93,89,61,40,251,187,46,
112,46,253,171,96,46,96,78,181,121,90,68,99,138,238,244,134,64,43,220,116,145,130,179,69,41,208,199,120,49,
135,151,234,51,180,205,241,118,86,108,63,229,251,132,44,138,24,121,254,54,228,99,233,162,104,137,219,182,194,154,
208,77,2,186,38,164,66,85,30,173,17,145,62,22,62,212,33,100,59,35,95,94,82,141,76,167,54,62,172,177,
12,160,41,226,13,1,9,95,142,180,84,112,41,32,120,214,135,136,67,151,132,117,69,74,150,184,108,61,121,19,
33,78,218,88,121,19,156,65,213,162,136,171,136,117,180,73,200,38,1,125,244,139,83,219,48,222,122,97,61,18,
50,129,161,12,159,16,83,20,113,221,177,114,4,26,195,197,154,121,114,214,7,45,226,144,205,239,170,54,6,27,
151,170,77,2,32,214,184,43,17,167,216,250,90,174,83,79,204,69,40,31,87,165,13,245,182,92,127,219,38,117,
199,202,231,2,189,146,197,152,239,86,139,56,100,160,62,246,105,124,33,34,158,237,240,20,192,186,195,9,109,146,
138,136,99,156,69,237,26,146,12,245,124,82,41,135,50,202,214,8,155,236,136,157,159,189,74,182,228,52,137,109,
133,33,207,171,114,225,187,236,23,47,37,120,10,69,10,132,204,27,168,98,189,199,154,14,37,66,191,181,36,94,
96,178,82,115,216,170,148,53,171,171,171,243,129,251,72,183,81,152,161,107,149,115,137,210,14,145,144,41,184,62,
170,134,35,103,3,26,234,24,67,154,109,18,122,208,0,218,208,208,212,212,212,211,34,182,46,16,66,242,64,137,
216,55,119,154,16,146,9,20,49,33,153,67,17,19,146,57,20,49,33,153,67,17,19,146,57,20,49,33,153,67,
17,19,146,57,20,49,33,153,67,17,19,146,57,20,49,33,153,67,17,19,146,57,174,141,242,8,73,141,233,194,
34,140,28,150,38,118,70,209,18,111,148,13,194,92,159,38,251,74,233,231,150,173,31,222,20,184,174,217,197,140,
35,189,197,79,157,221,246,33,105,114,61,175,201,26,219,105,57,109,175,108,5,207,182,6,229,225,75,243,206,192,
149,99,62,230,3,158,17,186,174,54,132,13,242,222,238,150,242,208,159,24,249,168,123,170,35,164,65,217,89,177,
41,222,76,131,58,167,9,122,143,69,75,172,23,225,187,214,14,55,105,253,102,141,3,147,215,90,87,119,49,231,
249,221,16,150,11,7,64,207,74,1,152,107,55,235,46,103,28,73,186,22,10,223,109,148,127,235,172,15,93,145,
52,111,240,252,189,22,129,107,33,125,221,52,107,154,188,199,145,164,125,67,201,18,209,153,72,75,238,54,74,157,
88,112,28,231,89,102,12,66,24,121,210,30,138,110,124,103,37,141,174,165,146,49,118,138,9,122,143,69,17,143,
26,108,77,82,198,72,206,165,221,228,201,180,118,149,154,188,124,51,205,115,17,243,225,42,147,101,249,190,137,53,
88,148,150,188,184,163,136,222,77,211,215,216,213,77,115,12,70,146,238,57,201,187,107,31,170,153,8,191,187,65,
68,224,219,155,185,233,6,21,163,8,141,217,178,209,16,187,68,60,138,208,152,5,189,71,211,157,214,173,75,236,
133,203,250,185,203,82,1,92,21,127,100,220,19,131,166,47,201,100,182,164,213,246,125,31,130,182,46,102,121,140,
164,161,91,223,240,217,101,105,110,130,126,238,130,120,58,174,197,240,211,13,69,166,27,199,243,172,43,241,243,81,
23,237,109,44,148,236,2,26,163,14,6,165,211,20,113,12,107,232,194,124,174,47,211,177,92,48,77,172,74,172,
43,169,235,101,140,34,88,132,162,24,182,137,117,115,253,94,40,101,105,110,130,249,220,37,71,3,132,72,134,96,
190,141,125,168,12,98,148,207,200,216,177,213,103,152,154,214,233,224,116,154,238,180,217,223,41,226,242,201,67,49,
91,164,21,169,0,69,23,36,134,235,161,137,89,137,125,30,130,110,136,92,253,217,113,208,101,49,47,47,125,169,
65,63,184,152,230,89,71,191,172,201,54,75,197,178,88,148,126,235,130,209,96,106,17,55,245,34,92,238,105,44,
124,239,116,28,70,133,114,152,47,116,19,99,24,196,224,247,104,138,120,198,115,198,236,142,134,34,46,182,72,139,
98,113,204,190,83,12,171,166,137,233,154,235,194,211,101,162,135,58,102,37,31,49,210,172,197,176,236,233,99,142,
75,49,205,38,77,196,81,244,110,22,141,6,72,215,143,166,174,52,60,110,232,168,16,241,94,110,80,39,99,184,
210,48,234,244,14,201,179,75,196,77,131,193,8,121,143,69,119,122,65,14,49,54,63,77,2,44,112,88,217,101,
199,78,151,49,221,233,152,253,65,29,116,154,145,143,222,87,120,109,196,141,244,151,3,78,164,31,7,45,164,226,
123,60,40,130,123,87,172,148,139,133,97,176,166,239,113,198,17,232,211,232,0,207,168,98,104,174,10,87,62,198,
193,101,101,23,10,155,254,197,168,207,193,239,177,40,226,54,250,33,174,231,154,153,118,21,74,19,154,190,164,98,
218,23,197,109,94,47,66,155,241,84,178,186,76,27,67,78,177,210,220,229,123,132,209,32,199,40,27,87,89,104,
111,112,193,115,125,28,92,249,104,250,247,69,195,20,51,50,93,137,22,113,219,145,233,226,115,205,76,231,20,153,
246,5,116,154,144,106,80,47,244,185,139,70,112,46,134,59,61,93,97,105,155,150,151,47,31,161,248,126,191,104,
152,58,137,76,195,16,113,23,145,233,34,11,198,96,185,235,122,93,218,142,76,47,24,129,168,24,164,26,212,11,
125,174,182,198,186,207,218,36,47,58,226,235,219,231,186,172,62,133,80,150,143,80,124,239,203,52,76,62,161,183,
146,78,83,196,93,185,96,26,221,199,137,41,226,46,34,211,75,198,32,127,12,202,202,40,86,154,219,126,238,162,
49,108,216,164,1,93,17,33,108,116,68,100,67,210,81,69,236,200,116,145,69,35,221,49,34,211,190,223,217,13,
211,157,246,205,63,110,50,255,179,170,69,90,108,161,15,139,136,145,105,95,133,188,48,240,244,130,16,98,7,245,
224,120,135,250,227,179,112,33,207,245,149,5,140,97,177,24,209,250,245,242,254,182,74,154,183,25,255,189,181,225,
111,196,142,76,23,49,167,135,198,136,76,187,222,161,245,30,245,9,16,197,85,34,38,43,37,137,174,194,23,105,
52,137,49,77,207,196,231,238,196,78,127,172,223,137,153,94,84,52,44,77,39,145,248,202,2,198,60,131,178,123,
198,97,84,136,68,47,75,57,197,136,250,54,73,99,213,251,210,177,129,166,233,12,122,143,83,83,83,248,127,23,
174,195,43,9,221,9,127,0,0,0,0,73,69,78,68,174,66,96,130,}
                , false))
            {
                Resources.UnloadAsset(logo);
                logo = null;
            }
        }

        #endregion

        Type entryType = Type.GetType("EntryEngine.Entry");
        if (entryType == null)
        {
            byte[] versionBytes = null;
            byte[] newFilelistBytes = null;

            #region 热更新

            if (!string.IsNullOrEmpty(ServerUrl))
            {
                var watch = System.Diagnostics.Stopwatch.StartNew();

                Debug.Log("正在检测版本更新");

                // 检查版本更新进行热更
                UnityWebRequest www = UnityWebRequest.Get(ServerUrl + VERSION + "?" + DateTime.Now.Ticks);
                www.timeout = 2;
                yield return www.SendWebRequest();
                if (string.IsNullOrEmpty(www.error))
                    versionBytes = www.downloadHandler.data;
                else
                    Debug.LogWarning("获取版本号异常：" + www.error);

                if (versionBytes != null)
                {
                    byte[] newVersionBytes = null;
                    if (!loader.Load(VERSION))
                        yield return loader.Wait;
                    byte[] oldVersionBytes = loader.Bytes;
                    // 没有旧版本号，必定更新新版本
                    if (oldVersionBytes == null)
                        newVersionBytes = versionBytes;
                    else
                    {
                        // 新旧版本号不同则需要更新版本
                        for (int i = 0; i < 8; i++)
                        {
                            if (oldVersionBytes[i] != versionBytes[i])
                            {
                                // 需要更新版本
                                newVersionBytes = versionBytes;
                                break;
                            }
                        }
                    }

                    if (newVersionBytes != null)
                    {
                        // 方便出错时break挑掉更新
                        while (true)
                        {
                            Debug.Log("正在计算更新包大小");

                            www = UnityWebRequest.Get(ServerUrl + FILE_LIST + "?" + DateTime.Now.Ticks);
                            yield return www.SendWebRequest();
                            if (!string.IsNullOrEmpty(www.error))
                            {
                                Debug.LogWarning("获取版本文件列表异常:" + www.error);
                                break;
                            }

                            if (!loader.Load(FILE_LIST))
                                yield return loader.Wait;
                            string[] oldList;
                            if (loader.Bytes != null)
                                oldList = ReadString(loader.Bytes).Split(SPLIT, StringSplitOptions.RemoveEmptyEntries);
                            else
                                oldList = new string[0];
                            newFilelistBytes = www.downloadHandler.data;
                            string[] newList = ReadString(newFilelistBytes).Split(SPLIT, StringSplitOptions.RemoveEmptyEntries);
                            // 用于记录已经更新的文件，中途关闭更新下次也能接着上次的更新
                            StringBuilder builder = new StringBuilder();

                            // 暂时不删除不需要了的文件，只列出需要下载的新文件或者需要更新的文件
                            Dictionary<string, FileNew> oldFilelist = new Dictionary<string, FileNew>();
                            foreach (var item in oldList)
                            {
                                string[] splits = item.Split('\t');
                                FileNew file = new FileNew();
                                file.File = splits[0];
                                file.Time = splits[1];
                                file.Length = long.Parse(splits[2]);
                                oldFilelist.Add(splits[0], file);
                            }

                            List<FileNew> newFilelist = new List<FileNew>();
                            foreach (var item in newList)
                            {
                                string[] splits = item.Split('\t');
                                FileNew file;
                                if (oldFilelist.TryGetValue(splits[0], out file))
                                {
                                    // 时间不一致需要更新
                                    if (file.Time != splits[1])
                                    {
                                        // 仅更新程序时，资源文件都不更新
                                        if (!All && !splits[0].EndsWith(".bytes"))
                                            continue;
                                        file.Length = long.Parse(splits[2]);
                                        newFilelist.Add(file);
                                    }
                                    else
                                    {
                                        // 旧文件不需要更新的写入builder
                                        builder.Append(item);
                                        builder.Append("\r\n");
                                    }
                                }
                                else
                                {
                                    // 仅更新程序时，资源文件都不更新
                                    if (!All && !splits[0].EndsWith(".bytes"))
                                        continue;
                                    // 需要下载的新文件
                                    file = new FileNew();
                                    file.File = splits[0];
                                    file.Time = splits[1];
                                    file.Length = long.Parse(splits[2]);
                                    newFilelist.Add(file);
                                }
                            }

                            // 可能只是删除了文件，导致没有需要更新的文件
                            total = newFilelist.Sum(f => f.Length);
                            if (newFilelist.Count > 0)
                            {
                                // 写入临时文件列表路径
                                string file_list = PersistentDataPath + FILE_LIST;

                                Debug.Log(string.Format("开始更新{0}个文件：共 {1} MB", newFilelist.Count, Byte2MB(total)));
                                // 同时开启多个下载
                                FileDownload[] asyncs = new FileDownload[8];
                                for (int i = 0; i < asyncs.Length; i++)
                                    asyncs[i] = new FileDownload();
                                long downloadBytesTemp = 0;
                                // 正在下载的字节数，下载完成的字节数才累计进入DownloadBytes
                                long downloading;
                                // 已经下载到的文件索引
                                int fileIndex = 0;
                                while (true)
                                {
                                    // 是否有新下好的文件
                                    bool downloadFlag = false;
                                    // 是否全部下载完成
                                    bool completeFlag = true;
                                    downloading = downloadBytesTemp;
                                    for (int i = 0; i < asyncs.Length; i++)
                                    {
                                        var async = asyncs[i].Web;
                                        if (async != null && !async.isDone)
                                        {
                                            // 正在下载
                                            FileNew file = asyncs[i].File;
                                            downloading += (long)(async.downloadProgress * file.Length);
                                            completeFlag = false;
                                        }
                                        else
                                        {
                                            // 未下载或者下载已经完成
                                            if (async != null)
                                            {
                                                FileNew file = asyncs[i].File;
                                                if (string.IsNullOrEmpty(async.error))
                                                {
                                                    WriteFile(file.File, async.downloadHandler.data);
                                                    downloadBytesTemp += file.Length;
                                                    downloading += file.Length;
                                                    // 每完成一个下载都写入旧文件列表，这样中途退出下次也能接着上次中断的文件开始下载
                                                    builder.Append(file.ToString());
                                                    downloadFlag = true;
                                                }
                                                else
                                                {
                                                    text.text = string.Format("下载文件{0}失败：{1}，可以重启程序后重试", file.File, async.error);
                                                    yield break;
                                                }
                                                asyncs[i].Web = null;
                                            }

                                            if (fileIndex == newFilelist.Count)
                                                continue;

                                            completeFlag = false;
                                            FileNew temp = newFilelist[fileIndex++];
                                            asyncs[i].File = temp;
                                            asyncs[i].Web = UnityWebRequest.Get(ServerUrl + temp.File + "?" + temp.Time);
                                            asyncs[i].Web.SendWebRequest();
                                            Debug.Log("开始下载" + temp.File);
                                        }
                                    }
                                    // 更新下载进度
                                    download = downloading;
                                    text.text = string.Format("正在下载更新 {0} MB / {1} MB", Byte2MB(download), Byte2MB(total));
                                    if (downloadFlag && (!completeFlag || !All))
                                        // 每完成一个下载都写入旧文件列表，这样中途退出下次也能接着上次中断的文件开始下载
                                        File.WriteAllText(file_list, builder.ToString());
                                    // 结束或等待下载
                                    if (completeFlag)
                                    {
                                        download = total;
                                        break;
                                    }
                                    else
                                        yield return null;
                                }

                                if (All)
                                    WriteFile(FILE_LIST, newFilelistBytes);
                            }

                            if (All)
                                WriteFile(VERSION, newVersionBytes);
                            Debug.Log(string.Format("版本更新完成 更新文件：{0}个 更新大小：{1} MB 版本号：{2}", newFilelist.Count, Byte2MB(total), BitConverter.ToInt64(newVersionBytes, 0)));

                            break;
                        }// end of while(true)
                    }// end of 需要更新新版本
                }// end of 新版本号不为null

                watch.Stop();
                Debug.Log(string.Format("版本更新耗时：{0}秒", watch.Elapsed.TotalSeconds));
            }// end of 服务器地址不为null
            #endregion

            #region 动态加载运行时

            Debug.Log("正在准备运行环境");

            if (!loader.Load(RUNTIME))
                yield return loader.Wait;
            if (loader.Bytes == null)
            {
                text.text = "加载运行环境失败，可以尝试删除__version.txt后重启以自动修复程序";
                yield break;
            }

            {
                try
                {
                    // 加载EntryEngine
                    Assembly assembly = AppDomain.CurrentDomain.Load(loader.Bytes, null);
                    // 调用dll自解析
                    Type type = assembly.GetType("Program");
                    MethodInfo method = type.GetMethod("Main", BindingFlags.Public | BindingFlags.Static);
                    method.Invoke(null, new object[1] { null });
                    Debug.Log("加载运行时环境完成");

                    // 为EntryEngine的第二步热更新资源设置好参数
                    if (!All)
                    {
                        assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.GetName().Name == "EntryEngine");
                        type = assembly.GetType("EntryEngine._HOT_FIX");
                        type.GetField("ServerURL", BindingFlags.Public | BindingFlags.Static).SetValue(null, ServerUrl);
                        type.GetField("NewVersionBytes", BindingFlags.Public | BindingFlags.Static).SetValue(null, versionBytes);
                        type.GetField("NewFileListBytes", BindingFlags.Public | BindingFlags.Static).SetValue(null, newFilelistBytes);
                    }
                }
                catch (Exception ex)
                {
                    text.text = "加载运行环境失败，可以尝试删除__version.txt后重启以自动修复程序";
                    Debug.LogWarning("加载运行时环境失败：" + ex.Message);
                    yield break;
                }
            }

            Debug.Log("正在准备引用动态库");
            if (!loader.Load(FILE_LIST))
                yield return loader.Wait;
            if (loader.Bytes == null)
            {
                text.text = "加载动态库文件列表失败，可以尝试删除__version.txt后重启以自动修复程序";
                yield break;
            }

            {
                string[] list = ReadString(loader.Bytes).Split(SPLIT, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < list.Length; i++)
                {
                    if (string.IsNullOrEmpty(list[i]))
                        continue;
                    string dllFile = list[i].Split('\t')[0];
                    if (!dllFile.EndsWith(".bytes"))
                        break;
                    // 不重复加载运行时
                    if (dllFile == RUNTIME)
                        continue;
                    if (!loader.Load(dllFile))
                        yield return loader.Wait;
                    AppDomain.CurrentDomain.Load(loader.Bytes, null);
                    Debug.Log(string.Format("加载{0}完成", dllFile));
                }
            }

            #endregion
        }

        #region 启动主程序

        var assemblies = AppDomain.CurrentDomain.GetAssemblies();
        Debug.Log("添加入口脚本");
        // 创建Unity入口
        Assembly unity = assemblies.FirstOrDefault(a => a.GetName().Name == "Unity");
        Type gateType = unity.GetType("EntryEngine.Unity.UnityGate");
        var gate = gameObject.AddComponent(gateType);

        Debug.Log("设置着色器");
        // 设置同名属性
        //Debug.Log(gateType.GetProperty("Gate", BindingFlags.Public | BindingFlags.Static).GetValue(null, new object[0]));  // 测试是否已经执行了Start: 没有执行
        //var fields = GetType().GetFields(BindingFlags.Public | BindingFlags.Instance);
        //for (int i = 0; i < fields.Length; i++)
        //    gateType.GetField(fields[i].Name, BindingFlags.Public | BindingFlags.Static).SetValue(null, fields[i].GetValue(this));
        gateType.GetField("GLMaterial", BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance).SetValue(gate, GLMaterial);

        Debug.Log("加载游戏入口动态库");
        // 加载游戏入口场景
        Assembly client = assemblies.FirstOrDefault(a => a.GetName().Name == "Client");
        if (client == null)
        {
            Debug.Log("未找到游戏动态库");
            yield break;
        }
        Debug.Log("加载游戏入口场景");
        // 使用入口展示场景实例 entry.ShowMainScene<T>();
        PropertyInfo entry = gateType.GetProperty("Entry");
        object __entry = entry.GetValue(gate, new object[0]);
        while (__entry == null)
        {
            // 等待帧结束，让入口Start调用完毕
            yield return null;
            __entry = entry.GetValue(gate, new object[0]);
        }
        Debug.Log("显示主场景");
        entry.PropertyType.GetMethod("ShowMainScene", Type.EmptyTypes).
            MakeGenericMethod(client.GetType("MAIN")).
            Invoke(__entry, new object[0]);

        #endregion

        Debug.Log("游戏开始");
        // 移除加载程序
        DestroyImmediate(this);
    }
    void OnGUI()
    {
        if (Event.current.type == EventType.Repaint)
        {
            if (logo != null)
            {
                if (logo.width > Screen.width || logo.height > Screen.height)
                {
                    GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), logo, ScaleMode.ScaleToFit);
                }
                else
                {
                    GUI.DrawTexture(new Rect(Screen.width / 2 - logo.width / 2, Screen.height / 2 - logo.height / 2, logo.width, logo.height), logo);
                }
            }

            if (!string.IsNullOrEmpty(text.text))
            {
                var size = GUIStyle.none.CalcSize(text);
                GUIStyle.none.Draw(new Rect(Screen.width / 2 - size.x / 2, Screen.height / 2 - size.y / 2 + 180, size.x, size.y), text, 0);
            }

            if (total > 0 && download < total)
            {
                // 进度条
                GUI.DrawTexture(new Rect(Screen.width / 4, Screen.height / 2 + 210, Screen.width / 2, 4), process, ScaleMode.StretchToFill, false, 0, Color.black, 0, 0);
                GUI.DrawTexture(new Rect(Screen.width / 4, Screen.height / 2 + 210, (float)(download * 1.0 / total) * Screen.width / 2, 4), process, ScaleMode.StretchToFill, false, 0, Color.white, 0, 0);
            }
        }
    }
}